/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ","ajaxServ","formServ","tabServ","domReady"], 
	function($,msgServ,ajaxServ,formServ,tabServ,domReady) {

	'use strict';

	var centerTabs='centerTabs',
		permissionUrl="getPermissions",rootPath='';
	
	var private_method = {
		
		/**
		 * 主页面导航栏按钮监听函数
		 */
		accordionClick : function(e) {
			
			var _url = ''
			var _text = ''
			if(e!=null){
				e.preventDefault();
				_url = $(e.currentTarget).attr('targetUrl');
				_text = e.currentTarget.text
				var _options = {
					target : centerTabs,
					title : _text,
					tabUrl : '/'+rootPath+_url,
					tools : []
				};
				if (_url) {
//					tabServ.addFrameTab(_options);
					tabServ.addHrefTab(_options)
				}
				
				
			}

//			event.preventDefault();
		}
	};
	domReady(function (doc) {
        console.info('main modules gets ready and  been loaded.');
        var patharr = doc.location.pathname.split('/')
        rootPath = patharr[1]
        
        var $ma=$("#navMenu");
        
       
       
		$ma.accordion({animate:true,fit:true,border:false});
		$.post(permissionUrl, null, function(rsp) {
			$.each(rsp,function(i,e){
				var menulist ="<div class=\"well well-small\">";
				if(e.child && e.child.length>0){
					$.each(e.child,function(ci,ce){
						var effort=ce.name+"||"+ce.iconCls+"||"+ce.url;
						menulist+="<a href=\"javascript:void(0);\" class=\"accordion-btn easyui-linkbutton\" data-options=\"plain:true,iconCls:'"+ce.iconCls + "'\"  bizcode='" + ce.bizcode + "'\"  targeturl='" + ce.pageUrl +"' data-type='accordion-btn'\>" +ce.name+"</a><br/>";
					});
				}
				menulist+="</div>";
				$ma.accordion('add', {
		            title: e.name,
		            content: menulist,
					border:false,
		            iconCls: e.iconCls,
		            selected: false
		        });
			});
			
			$("#navMenu a.accordion-btn").bind('click',function(e){
				private_method.accordionClick(e)
			})
		}, "JSON").error(function() {
			$.messager.alert("提示", "获取菜单出错,请重新登陆!");
		});	
		
		var menuTab = $(doc).find('#'+centerTabs)
		$(menuTab).tabs({
			tools : [ {
					iconCls : 'icon-reload',
					handler : function() {
						var href = $('#'+centerTabs).tabs('getSelected').panel('options').href;
						if (href) {/*说明tab是以href方式引入的目标页面*/
							var index = $('#centerTabs').tabs('getTabIndex', $('#centerTabs').tabs('getSelected'));
							$('#centerTabs').tabs('getTab', index).panel('refresh');
						} else {   /*说明tab是以content方式引入的目标页面*/
							var panel = $('#centerTabs').tabs('getSelected').panel('panel');
							var frame = panel.find('iframe');
							try {
								if (frame.length > 0) {
									for ( var i = 0; i < frame.length; i++) {
										frame[i].contentWindow.document.write('');
										frame[i].contentWindow.close();
										frame[i].src = frame[i].src;
									}
									if ($.browser.msie) {
										CollectGarbage();
									}
								}
							} catch (e) {
							}
						}
					}
				}, {
					iconCls : 'icon-cancel',
					handler : function() {
						var index = $('#centerTabs').tabs('getTabIndex', $('#centerTabs').tabs('getSelected'));
						var tab = $('#centerTabs').tabs('getTab', index);
						if (tab.panel('options').closable) {
							$('#centerTabs').tabs('close', index);
						} else {
							$.messager.alert('提示', '[' + tab.panel('options').title + ']不可以被关闭', 'error');
						}
         			}
        		},{
        		   iconCls:'icon-color',
                   handler:function(){
	                   $('#theme').menu({   
							onClick:function(item){   
	                        var cookiesColor1=jqueryUtil.cookies.get("cookiesColor");
	                        	if(cookiesColor1!=item.id){
									jqueryUtil.cookies.set("cookiesColor",item.id,30);
									jqueryUtil.chgSkin(item.id,cookiesColor1);
	                            }
							}   
							});
	                    $('#theme').menu('show', {   
								 left: '91%',   
								 top: 97   
						});
	                    }
        		}],
				fit : true,
				border : false,
				onContextMenu : function(e, title) {
					e.preventDefault();
					tabsMenu.menu('show', {
						left : e.pageX,
						top : e.pageY
					}).data('tabTitle', title);
				}

			});
			
			
				
    });
    
	
	var moduleConf = {}
	
	var public_method = {		

		setModuConf:function(modu_name,modu_config){
			moduleConf[modu_name]=modu_config
		},
		getModuConf:function(modu_name){
			return moduConf[modu_name]
		}

	};
	
	return public_method

});